<template>
  <div class="mod-activity">
    <el-form :inline="true" :model="searchForm" @keyup.enter.native="pageIndex = 1;getDataList()">
      <el-form-item>
        <el-input v-model="searchForm.name" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="pageIndex = 1; getDataList()">查询</el-button>
        <el-button v-if="isAuth('mall:activity:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('mall:activity:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="名称">
      </el-table-column>
      <el-table-column
        prop="activityCode"
        header-align="center"
        align="center"
        label="活动标识">
      </el-table-column>
      <el-table-column
        prop="isShow"
        header-align="center"
        align="center"
        label="是否展示">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isShow === 0" size="small" type="danger">否</el-tag>
          <el-tag v-else-if="scope.row.isShow === 1" size="small" type="success">是</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="activityType"
        header-align="center"
        align="center"
        label="活动类型">
        <template slot-scope="scope" >
          <template v-for="item in activityTypeList">
            <span v-if="scope.row.activityType==item.value " >{{item.name}}</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column
        prop="whitelistConfine"
        header-align="center"
        align="center"
        label="限制规则">
        <template slot-scope="scope" >
          <template v-for="item in whitelistConfineList">
            <span v-if="scope.row.whitelistConfine==item.value " >{{item.name}}</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column
        prop="isShow"
        header-align="center"
        align="center"
        label="是否付费">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.needToPay === 0" size="small" type="danger">否</el-tag>
          <el-tag v-else-if="scope.row.needToPay === 1" size="small" type="success">是</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="activityDate"
        header-align="center"
        align="center"
        label="活动日期">
      </el-table-column>
      <el-table-column
        prop="activityTime"
        header-align="center"
        align="center"
        label="活动具体时间">
      </el-table-column>
      <el-table-column
        prop="goodsTag"
        header-align="center"
        align="center"
        label="微信优惠标识">
      </el-table-column>
      <el-table-column
        fixed="right"
        prop=""
        header-align="center"
        align="center"
        label="关联店铺">
        <template slot-scope="scope">
          <el-button v-if="isAuth('mall:activity:update')" type="text" size="small"
                     @click="transferShow(scope.row.id)">关联店铺</el-button>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('mall:activity:info')" type="text" size="small" @click="showGoodsDetails(scope.row.id)">缓存信息</el-button>
          <el-button v-if="isAuth('mall:activity:info')" type="text" size="small" @click="showDetails(scope.row.id)">查看</el-button>
          <el-button v-if="isAuth('mall:activity:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button v-if="isAuth('mall:activity:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>

    <!-- 显示活动的缓存信息   -->
    <el-dialog title="活动详情" :visible.sync="goodsDetailsVisible">
      <div>
        <h3>活动日期（最近一次）</h3>
        <div style="float: right">
          <el-button @click="delCache" type="danger">删除缓存</el-button>
        </div>
        <div style="margin-left: 25px;">
          {{details.activityDate?details.activityDate.join(' --> '):''}}
        </div>
      </div>
      <div v-for="(v,k) in details.activityTime">
        <h3>活动时间 {{v.join(' --> ')}}</h3>
        <div style="margin-left: 25px">
          <el-table :data="details.goodsData[k]">
            <el-table-column property="goodsImage" label="图片">
              <template slot-scope="scope">
                <img style="height: 50%;width: 50%" @click="openImg(scope.row.imgUrl)" :src="scope.row.goodsImage"/>
              </template>
            </el-table-column>
            <el-table-column property="goodsName" label="商品名称" width="150"></el-table-column>
            <el-table-column property="expendCount" label="商品库存（缓存）" width="200">
              <template slot-scope="scope">
                <span>{{details.stock[scope.row.id]}}</span>
              </template>
            </el-table-column>
<!--            <el-table-column property="expendCount" label="商品库存" width="200"></el-table-column>-->
            <el-table-column property="goodsPrice" label="价格"></el-table-column>
            <el-table-column property="price" label="活动价格"></el-table-column>
          </el-table>
        </div>
      </div>
    </el-dialog>

    <card-transfer
      v-if="transferVisible"
      :getBinding="{
          url: `/mall/activity/getShopsIds`,
          data: (target)=>{ return {activityId: target.id}},
          getId: (target)=>{return target}
        }"
      :save="{
          url: `/mall/activity/saveShopsToActivty`,
          data: (target)=>{ return {
            'activtyId': target.id,
            'shopsIds': target.shopsList
          }}
        }"
      ref="cardTransfer">
    </card-transfer>
  </div>
</template>

<script>
import AddOrUpdate from './activity-add-or-update'
import CardTransfer from './card-transfer'



export default {
  data() {
    return {
      activityId: '',
      goodsDetailsVisible: false,
      details: [],
      searchForm: {
        name: ''
      },
      dataList: [],
      pageIndex: 1,
	    whitelistConfineList:[],
      activityTypeList:[],
      pageSize: 10,
      totalPage: 0,
      transferVisible: false,
      dataListSelections: [],
      addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate,CardTransfer
    },
    activated () {
      this.getDataList()
      this.dist()
    },
    methods: {
      dist(){
        // 远程请求回来的数据
        this.$http({
          url: '/sys/dict/queryByCode',
          method: 'get',
          params: {
            'code': 'WHITELIST_CONFINE'
          }
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.whitelistConfineList = data.list
          }
        })

        this.$http({
          url: '/sys/dict/queryByCode',
          method: 'get',
          params: {
            'code': 'ACTIVITY_TYPE'
          }
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.activityTypeList = data.list
          }
        })
      },
      jsonParse(obj){
        return JSON.parse(obj)
      },
      delCache(){
        this.$confirm(`确定对活动缓存进行[删除]操作?删除可能会有风险请自行承担！`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
        this.$http({
          url: '/mall/activity/delCache?activityId='+this.activityId,
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.goodsDetailsVisible = false
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500
            })
            this.getDataList()
          }
        })
        }).catch(() => {
        })
      },
      showGoodsDetails(id){
        this.activityId = id
        this.$http({
          url: '/mall/activity/getDetails?activityId='+id,
          method: 'get'
        }).then(({data}) => {
          this.goodsDetailsVisible = true
          console.log(data)
          this.details = data.data
          this.details.activityTime = JSON.parse(this.details.activityTime)
        })
      },
      transferShow (id) {
        this.transferVisible = true
        this.$nextTick(() => {
          this.$refs.cardTransfer.init(id)
        })
      },
      // 获取数据列表
      getDataList () {

          this.$http({
            url: '/mall/activity/list',
            method: 'get',
            params: {
              'page': this.pageIndex,
              'limit': this.pageSize,
              'name': this.searchForm.name
            }
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataList = data.page.records
              this.totalPage = data.page.total
            } else {
              this.dataList = []
              this.totalPage = 0
            }
          })

      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 查看详情
      showDetails (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, true)
        })
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        let ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[删除]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: '/mall/activity/delete',
            method: 'post',
            data: ids
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.getDataList()
            }
          })
        }).catch(() => {
        })
      }
    }
  }
</script>
